/**
 * This class reads the output of Linear regression algorithm implemented by Minining algorithms and
 * stores theat into an class and then return it to DrawGraph class to plot the graphs 
 */
package org.wmine.visualization.data;

import java.io.File;
import java.io.IOException;
import java.io.StringBufferInputStream;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.wmine.core.ProjectManager;
import org.wmine.visualization.data.LinearRegData.LinearRegSingleData;
import org.wmine.visualization.*;
import org.xml.sax.SAXException;

/**
 * @author Rajeev Kumar Thakur
 *
 */
public class LinearRegDataProvider extends AbstractChartDataProvider {
	
	
	private Document xmlDocument;
	LinearRegData linearRegData;
	ArrayList<LinearRegSingleData>  dataList;
	public LinearRegDataProvider(String name) throws ParserConfigurationException, SAXException, IOException {
		
		linearRegData = new LinearRegData();
		dataList = new ArrayList<LinearRegSingleData>();
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder parser = factory.newDocumentBuilder();
		if(ProjectManager.getUniqueInstance().isProjectOpened())
		{xmlDocument = (Document) parser.parse(new StringBufferInputStream(ProjectManager.getUniqueInstance().readFile(name)));
		parseOutputData();
		}
	}
	
	public void parseOutputData() {
		NodeList statList=null;
		if(ProjectManager.getUniqueInstance().isProjectOpened())
			 statList= xmlDocument.getElementsByTagName("stat-result");
		int no = statList.getLength();
		//System.out.println(no);
		// TODO Auto-generated method stub
		for (int i = 0; i < no; i++) {
			
			NodeList linearRegList = xmlDocument.getElementsByTagName("Linear-Regression");
			int no1 = linearRegList.getLength();
			//System.out.println("in LinearReg Data provider: length linear regression" +no1);
			for (int j = 0; j < no1; j++) {
				
				NodeList resultList = xmlDocument.getElementsByTagName("resultlr");
				//System.out.println("in LinearReg Data provider: length linear regression result "+resultList.getLength());
				for (int k = 0; k < resultList.getLength(); k++) {
					Element elementA = (Element) resultList.item(k);
					LinearRegSingleData singleResult = linearRegData.getLinearRegSingleDataInstance();
					singleResult.setAttribute1(getElementFromXmlFile(elementA,"attribute1"));
				///	System.out.print("attribute 1 :"+singleResult.getAttribute1()+" ");
					singleResult.setAttribute2(getElementFromXmlFile(elementA,"attribute2"));
					//System.out.print("attribute 2 :"+singleResult.getAttribute2()+" ");
					singleResult.setSlope(Double.parseDouble(getElementFromXmlFile(elementA,"slope")));
					//System.out.println("\nSlope :"+singleResult.getSlope());
					singleResult.setIntercept(Double.parseDouble(getElementFromXmlFile(elementA,"intercept")));
					//System.out.println("intercept :"+singleResult.getIntercept());
					dataList.add(singleResult);
				}
				linearRegData.setLinearRegDataList(dataList);
			}
		}
		
	}
	public String getElementFromXmlFile(Element elementA, String tagName){
		NodeList temp = (NodeList) elementA.getElementsByTagName(tagName);
		Element subElement = (Element) temp.item(0);
		return subElement.getFirstChild().getNodeValue();
	}
	public @Override
	Object getChartData() {
		// TODO Auto-generated method stub
		return linearRegData;
	}
	
	/*
	public static void main(String[] args) {
		AbstractChartDataProvider freq;
		try {
			freq = new LinearRegDataProvider("D:\\Project_WMine\\WMine\\stat-result.xml");
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	*/
}
